package service.impl;

import dao.ArticleDao;
import dao.FavoriteDao;
import model.Article;
import model.Favorite;
import pojo.PageEntity;
import service.FavoriteService;
import utils.PageUtil;

import java.util.List;
import java.util.Map;

public class FavoriteServiceImpl implements FavoriteService {

    private FavoriteDao favoriteDao = new FavoriteDao(Favorite.class);
    private ArticleDao articleDao = new ArticleDao(Article.class);
    @Override
    public List<Map<String, Object>> findByUserFavorite(Integer id, Integer artId) {
        return favoriteDao.findByUserFavorite(id,artId);
    }

    @Override
    public Boolean findIsFavorite(Integer id, Integer artId) {
        Integer count = articleDao.countByUserIdAndArticleId(id, artId);
        return count != 0 ? true : false;
    }

    @Override
    public Integer findByFavoriteCountByArticleId(Integer artId) {
        return favoriteDao.findByFavoriteCountByArticleId(artId);
    }

    @Override
    public Integer unFavorite(Integer id, Integer artId) {
        return favoriteDao.unFavorite(id,artId);
    }

    @Override
    public Integer favorite(Integer id, Integer artId) {
        return favoriteDao.favorite(id,artId);
    }

    @Override
    public PageEntity<Article> favoritePage(Integer currPage, String title, Integer id) {
        PageEntity<Article> pageModel = new PageEntity<>();
        Integer pageSize = pageModel.getPageSize();
        List<Article> data = favoriteDao.favoritePage(id, title, (currPage - 1) * pageSize, pageSize);
        Integer totalCount = countByFavorite(id,title);
        pageModel = PageUtil.createArtPage(currPage,data,totalCount);
        return pageModel;
    }

    private Integer countByFavorite(Integer id, String title) {
        return favoriteDao.countByFavorite(id,title);
    }
}
